<html>
<head><meta charset="utf-8"><title>Getting method defid from ExprKind::MethodCall · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20method.20defid.20from.20ExprKind.3A.3AMethodCall.html">Getting method defid from ExprKind::MethodCall</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="234347589"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20method%20defid%20from%20ExprKind%3A%3AMethodCall/near/234347589" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20method.20defid.20from.20ExprKind.3A.3AMethodCall.html#234347589">(Apr 13 2021 at 15:25)</a>:</h4>
<p>I currently have the current code:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="n">ExprKind</span>::<span class="n">MethodCall</span><span class="p">(</span><span class="n">segment</span><span class="p">,</span><span class="w"> </span><span class="n">method_span</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">,</span><span class="w"> </span><span class="n">_</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">debug</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">env</span>::<span class="n">var</span><span class="p">(</span><span class="s">"LOL"</span><span class="p">).</span><span class="n">is_ok</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">hir_id</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">segment</span><span class="p">.</span><span class="n">hir_id</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">hir</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">hir</span><span class="p">();</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">body_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">hir</span><span class="p">.</span><span class="n">enclosing_body_owner</span><span class="p">(</span><span class="n">hir_id</span><span class="p">);</span><span class="w"></span>
<span class="w">        </span><span class="kd">let</span><span class="w"> </span><span class="n">typeck_results</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">tcx</span><span class="p">.</span><span class="n">typeck_body</span><span class="p">(</span><span class="n">hir</span><span class="p">.</span><span class="n">maybe_body_owned_by</span><span class="p">(</span><span class="n">body_id</span><span class="p">).</span><span class="n">unwrap</span><span class="p">());</span><span class="w"></span>
<span class="w">        </span><span class="c1">// For now, this part doesn't work...</span>
<span class="w">        </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">typeck_results</span><span class="p">.</span><span class="n">type_dependent_def_id</span><span class="p">(</span><span class="n">hir_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">            </span><span class="k">match</span><span class="w"> </span><span class="n">hir</span><span class="p">.</span><span class="n">span_if_local</span><span class="p">(</span><span class="n">def_id</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="nb">Some</span><span class="p">(</span><span class="n">span</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="bp">self</span><span class="p">.</span><span class="n">matches</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="w"></span>
<span class="w">                        </span><span class="n">span_to_tuple</span><span class="p">(</span><span class="n">method_span</span><span class="p">),</span><span class="w"></span>
<span class="w">                        </span><span class="n">LinkFromSrc</span>::<span class="n">Local</span><span class="p">(</span><span class="n">span</span><span class="p">),</span><span class="w"></span>
<span class="w">                    </span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="nb">None</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="bp">self</span><span class="p">.</span><span class="n">matches</span><span class="p">.</span><span class="n">insert</span><span class="p">(</span><span class="w"></span>
<span class="w">                        </span><span class="n">span_to_tuple</span><span class="p">(</span><span class="n">method_span</span><span class="p">),</span><span class="w"></span>
<span class="w">                        </span><span class="n">LinkFromSrc</span>::<span class="n">External</span><span class="p">(</span><span class="n">def_id</span><span class="p">),</span><span class="w"></span>
<span class="w">                    </span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="p">}</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
<span class="w">        </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>In the <a href="https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/hir/enum.ExprKind.html#variant.MethodCall">documentation</a>, it is said to use <a href="https://doc.rust-lang.org/beta/nightly-rustc/rustc_middle/ty/context/struct.TypeckResults.html#method.type_dependent_def_id">TypeckResults::type_dependent_def_id</a> to get it but for now <code>type_dependent_def_id</code> always return <code>None</code>. My guess is that I badly get the <code>BodyId</code> which is needed by <code>tcx.typeck_body</code>.</p>
<p>If anyone has an idea on how to fix it, it'd be very welcome!</p>
<p>Thanks in advance!</p>



<a name="234386834"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Getting%20method%20defid%20from%20ExprKind%3A%3AMethodCall/near/234386834" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> GuillaumeGomez <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Getting.20method.20defid.20from.20ExprKind.3A.3AMethodCall.html#234386834">(Apr 13 2021 at 19:31)</a>:</h4>
<p>I was able to get the info I wanted. Last issue: it displays errors (they don't stop the process to run so perfect), trying to hide them now. :)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>